<template>
  <div class="app-container">
    <!-- 添加或修改报修收费标准对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属小区" prop="communityId">
          <el-select v-model="form.communityId" placeholder="请选择所属小区"  :disabled=disabled>
            <el-option
              v-for="dict in communityOptions"
              :key="dict.id"
              :label="dict.communityName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人工费">
          <imageUpload v-model="form.labourCost"/>
        </el-form-item>
        <el-form-item label="材料费">
          <imageUpload v-model="form.materialCost"/>
        </el-form-item>
        <el-form-item label="机械费">
          <imageUpload v-model="form.mechanicalFee"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="disabled"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCommunity  } from "@/api/property/community";
  import { getRepairFeeScale , addRepairFeeScale, updateRepairFeeScale } from "@/api/workorder/repairFeeScale";

  export default {
    name: "RepairFeeScaleFrom",
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          communityId: [
            {required: true, message: "所属小区不能为空", trigger: "blur"}
          ],
        },
        communityOptions:[]
      };
    },
    created() {
      this.getCommunityOptionsList()
    },
    methods: {
      show(option) {
        this.reset();
        this.disabled = option.type == 'VIEW'
        if (option.type == 'ADD') {
          this.open = true;
          this.title = "新增报修收费标准";
        } else if (option.type == 'UPDATE' || option.type == 'VIEW') {
          getRepairFeeScale(option.id).then(response => {
            this.form = response.data;

            if(option.type == 'VIEW'){
              this.title = "查看报修收费标准";
            }else{
              this.title = "修改报修收费标准";
            }
            this.open = true;
          });
        }
      },
      //小区列表
      getCommunityOptionsList(){
        listCommunity().then(response => {
          this.communityOptions =response.rows;
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          repairProjectId: this.repairProjectId,
          communityId: null,
          labourCost: null,
          materialCost: null,
          mechanicalFee: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRepairFeeScale(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addRepairFeeScale(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      }
    },
    props: {
      repairProjectId: {
        type: String,
      },
    }
  };
</script>
